home *** CD-ROM | disk | FTP | other *** search
/ Aminet 40 / Aminet 40 (2000)(Schatztruhe)[!][Dec 2000].iso / Aminet / util / cdity / ModeProSrc.lha / Daemon / Unnamed1 / 4.1 / MPMain.c < prev    next >
C/C++ Source or Header  |  1996-06-12  |  16KB  |  575 lines

  1. #include "MP.h"
  2.  
  3.  
  4. #include <utility/hooks.h>
  5. #include <intuition/sghooks.h>
  6.  
  7. #include <proto/locale.h>
  8.  
  9. long __stack = 4096;
  10.  
  11. UBYTE EnvFile[]   ="ENV:ModePro.pref",
  12.       EnvArcFile[]="ENVARC:ModePro.pref";
  13.        
  14. #define MPVER  "4.1"
  15.  
  16. /* MPSem VER.REV */
  17. #define VER     4
  18. #define REV     1
  19.  
  20. UBYTE   Version[]   ="$VER: ModePro "MPVER" "__AMIGADATE__;
  21. UBYTE   ModePro[]   ="ModePro "MPVER;
  22.  
  23. BYTE PublicSignal;
  24. struct Process *MPTask;
  25.  
  26. struct Screen ASM *(*OldOpenScreen)(REG __a0 struct NewScreen *,
  27.                                     REG __a6 struct IntuitionBase *);
  28. struct Screen ASM *(*OldOpenScreenTagList)(REG __a0 struct ExtNewScreen *ns,
  29.                                            REG __a1 struct TagItem *taglist,
  30.                                            REG __a6 struct IntuitionBase * );
  31. BOOL ASM (*OldCloseScreen)(REG __a0 struct Screen *,
  32.                            REG __a6 struct IntuitionBase *);
  33. struct Window ASM *(*OldOpenWindow)(REG __a0 struct NewWindow *nw,
  34.                                     REG __a6 struct IntuitionBase *lib);
  35. struct Window ASM *(*OldOpenWindowTagList)(REG __a0 struct NewWindow *nw,
  36.                                            REG __a1 struct TagItem *tags,
  37.                                            REG __a6 struct IntuitionBase *lib);
  38. BOOL ASM *(*OldLayoutMenusA)(REG __a0 struct Menu *fm,
  39.                              REG __a1 APTR vi, 
  40.                              REG __a2 struct TagItem *tags,
  41.                              REG __a6 struct Library *lib);
  42. LONG ASM *(*OldCloseWorkBench)(REG __a6 struct IntuitionBase *lib);
  43.  
  44.  
  45.  
  46. BOOL    V39;
  47. ULONG   Deinstall=FALSE;
  48.                 
  49. struct NewBroker NewBroker=
  50. {
  51.   NB_VERSION,
  52.   "ModePro",
  53.   ModePro,
  54.   0,
  55.   NBU_UNIQUE | NBU_NOTIFY,
  56.   COF_SHOW_HIDE,
  57.   0,0,0
  58. };
  59.  
  60.  
  61.  
  62. UWORD NumDriPens;
  63.  
  64. /*
  65. UWORD defaultpens[]=
  66. {
  67.   0,1,1,2,1,3,1,0,2,1,2,1
  68. };
  69. */
  70.  
  71. struct TextAttr Topaz8 = { "topaz.font",8,0,0};
  72.  
  73.  
  74.  
  75. struct MsgPort *BrokerPort,
  76.                *CatchPort,
  77.                *ReplyPort;
  78. struct Message *ms;
  79.  
  80. struct IntuitionBase *IntuitionBase; 
  81. struct GfxBase       *GfxBase;
  82. struct Library       *AslBase,
  83.                      *CxBase,
  84.                      *DiskfontBase,
  85.                      //*GadToolsBase,
  86.                      *IconBase,
  87.                      *IFFParseBase,
  88.                      *LocaleBase,
  89.                      *UtilityBase;
  90.  
  91. struct Libs MP_Libs[]=
  92. {
  93.   (APTR *)&AslBase,      "asl.library",        38,
  94.   (APTR *)&CxBase,       "commodities.library",36,
  95.   (APTR *)&DiskfontBase, "diskfont.library",   36,
  96. //  (APTR *)&GadToolsBase, "gadtools.library",   36,
  97.   (APTR *)&GfxBase,      "graphics.library",   36,
  98.   (APTR *)&IconBase,     "icon.library",       36,
  99.   (APTR *)&IFFParseBase, "iffparse.library",   36,
  100.   (APTR *)&IntuitionBase,"intuition.library",  36,
  101. //  (APTR *)&LocaleBase,   "locale.library",     38,
  102.   (APTR *)&UtilityBase,  "utility.library",    36,
  103.   0,0,0
  104. };
  105.  
  106. CxObj   *Broker;
  107. CxMsg   *cxmsg;
  108. ULONG   cxsig;
  109. ULONG   sigrcvd;
  110.  
  111. /*** Tooltypes ***/
  112.  
  113. UBYTE *Hotkey="lcommand p",
  114.       Popup=TRUE;
  115. BYTE  Priority=0;
  116. //UBYTE *UserFontName=0;
  117. //LONG  UserFontSize=8;
  118. STRPTR PrefsPath;
  119.  
  120. struct Catalog *Catalog;
  121.  
  122. struct MPSem *MPSem;
  123.  
  124. void main(int argc, char **argv) 
  125. {
  126.   ULONG l;
  127.   struct Screen *ps;
  128.   struct DrawInfo *di;
  129.  
  130.   if(LocaleBase=OpenLibrary("locale.library",38))
  131.     Catalog=OpenCatalog(NULL,(STRPTR)"modepro.catalog", TAG_DONE);
  132.   else
  133.     Catalog=NULL;
  134.   
  135.   InitStrings();
  136.  
  137.   if(OpenLibs(ModePro,MP_Libs))
  138.   {
  139.     if(MPSem=AllocVec(sizeof(struct MPSem),MEMF_PUBLIC|MEMF_CLEAR))
  140.     {
  141.       NewBroker.nb_Descr=GetString(MSG_COMMODITY_TITLE);
  142.   
  143.       if(((struct Library *)IntuitionBase)->lib_Version>38)
  144.       {
  145.         NumDriPens=12;
  146.         V39=TRUE;
  147.       }
  148.       else
  149.       {
  150.         NumDriPens=9;
  151.         V39=FALSE;
  152.       }
  153.       if(GetArgs(argc,argv))
  154.       {
  155.         {
  156.           struct SignalSemaphore *sem;
  157.         
  158.           sem=&MPSem->SharedSem;
  159.           for(l=0;l<SEMAPHORES;l++)
  160.             InitSemaphore(&sem[l]);
  161.         }
  162.   
  163.         MPSem->SharedSem.ss_Link.ln_Name =(UBYTE *)"ModePro";
  164.         MPSem->SharedSem.ss_Link.ln_Pri  =0;
  165.         MPSem->Enabled =TRUE;
  166.         MPSem->Version =VER;
  167.         MPSem->Revision=REV;
  168.         MPSem->HotKey  =Hotkey;
  169. /*  
  170.         if(ps=LockPubScreen(NULL))
  171.         {
  172.           di=GetScreenDrawInfo(ps);
  173.           for(l=0;l<di->dri_NumPens && l<NumDriPens;l++)
  174.             defaultpens[l]=di->dri_Pens[l];      
  175.           FreeScreenDrawInfo(ps,di);
  176.           UnlockPubScreen(NULL,ps);
  177.         }
  178. */  
  179.         if(BrokerPort=CreateMsgPort())
  180.         { 
  181.           NewBroker.nb_Pri=Priority;
  182.           NewBroker.nb_Port=BrokerPort;
  183.           if(Broker=CxBroker(&NewBroker,NULL))
  184.           {
  185.             AddHotKey(Broker,BrokerPort,Hotkey,EVT_HOTKEY);
  186.             if(CatchPort=CreateMsgPort())
  187.             {
  188.               if(MPSem->DaemonPort=CreateMsgPort())
  189.               {
  190.                 if(ReplyPort=CreateMsgPort())
  191.                 {
  192.                   if((PublicSignal=AllocSignal(-1))>=0)
  193.                   {
  194.                     MPTask=(struct Process *)FindTask(0);
  195.                     cxsig=1L<<BrokerPort->mp_SigBit;
  196.                     ActivateCxObj(Broker,1);
  197.           
  198.                     NewList(&MPSem->OpenList);
  199.                     NewList(&MPSem->PromotionList[0]);
  200.                     NewList(&MPSem->PromotionList[1]);
  201.                     
  202.                     if(!DaemonLoadFile(EnvFile))
  203.                         DaemonLoadFile(EnvArcFile);
  204.                         
  205.                     Forbid();
  206.                     OldCloseScreen       =SetFunction((struct Library *)IntuitionBase,-(0x42) ,(FUNCCAST)NewCloseScreen);
  207.                     OldOpenScreenTagList =SetFunction((struct Library *)IntuitionBase,-(0x264),(FUNCCAST)NewOpenScreenTagList);
  208.                     OldOpenScreen        =SetFunction((struct Library *)IntuitionBase,-(0xc6) ,(FUNCCAST)NewOpenScreen);
  209.                     OldOpenWindowTagList =SetFunction((struct Library *)IntuitionBase,-(0x25e),(FUNCCAST)NewOpenWindowTagList);
  210.                     OldOpenWindow        =SetFunction((struct Library *)IntuitionBase,-(0xcc) ,(FUNCCAST)NewOpenWindow);
  211.                     OldCloseWorkBench    =SetFunction((struct Library *)IntuitionBase,-(0x4e) ,(FUNCCAST)NewCloseWorkBench);
  212.                     OldLayoutMenusA      =SetFunction((struct Library *)GadToolsBase ,-(0x42) ,(FUNCCAST)NewLayoutMenusA);
  213.                     
  214.                     Permit();
  215.                  
  216.                     AddSemaphore((struct SignalSemaphore *)MPSem);
  217.                     ProcessInput();
  218.                     RemSemaphore((struct SignalSemaphore *)MPSem);
  219.                     
  220.                     ObtainSemaphore(&MPSem->ListSem);
  221.                     ReleaseSemaphore(&MPSem->ListSem);
  222.                     ObtainSemaphore(&MPSem->PrefsSem);
  223.                     ReleaseSemaphore(&MPSem->PrefsSem);
  224.   
  225.                     ProcessMPMessages();
  226.   
  227.                     {
  228.                       struct MPMessage *mpm;
  229.                       while(mpm=(struct MPMessage *)GetMsg(ReplyPort))
  230.                         FreeVec(mpm);
  231.                     }
  232.   
  233.                     FreeDefList(&MPSem->PromotionList[0]);
  234.                     FreeDefList(&MPSem->PromotionList[1]);
  235.                     FreeSignal(PublicSignal);
  236.                   }
  237.                   DeleteMsgPort(ReplyPort);
  238.                 }
  239.                 DeleteMsgPort(MPSem->DaemonPort);
  240.               }
  241.               DeleteMsgPort(CatchPort);
  242.             }
  243.             DeleteCxObjAll(Broker);
  244.             while(cxmsg=(CxMsg *)GetMsg(BrokerPort))
  245.               ReplyMsg((struct Message *)cxmsg);
  246.           }/* endif CxBroker() */
  247.           DeleteMsgPort(BrokerPort);
  248.         }// endif CreateMsgPort(BrokerPort)
  249.         // ArgArrayDone();
  250.         CleanupArgs(argc);
  251.       }// endif GetArgs() 
  252.       FreeVec(MPSem);
  253.     }
  254.     CloseLibs(MP_Libs);
  255.   } 
  256.   if(LocaleBase)
  257.   {
  258.     CloseCatalog(Catalog);
  259.     CloseLibrary(LocaleBase);
  260.   }
  261. }
  262.   
  263. void ProcessInput(void)
  264. {
  265.  
  266.   struct DefaultNode *catdnode;
  267.   ULONG  replysig,portsig,catchsig,signal,msgtype,pubsig,allsigs,daemonsig;
  268.   LONG msgid;
  269.  
  270.   
  271.   daemonsig=1<< MPSem->DaemonPort->mp_SigBit;
  272.   catchsig=1 << CatchPort->mp_SigBit;
  273.   portsig =1 << BrokerPort->mp_SigBit;
  274.   pubsig  =1 << PublicSignal; 
  275.   replysig=1 << ReplyPort->mp_SigBit;
  276.  
  277.   allsigs=daemonsig | replysig | pubsig | catchsig | portsig | SIGBREAKF_CTRL_C;
  278.  
  279.   if(Popup)
  280.     ShowGUI();
  281.  
  282.   while (!Deinstall)
  283.   {
  284.     signal=Wait (allsigs);
  285.  
  286.     if(SIGBREAKF_CTRL_C & signal)
  287.       CheckToQuit();
  288.  
  289.     if(daemonsig & signal)
  290.       ProcessMPMessages();
  291.  
  292.     if(replysig & signal)
  293.     {
  294.       struct MPMessage *mpm;
  295.       
  296.       while(mpm=(struct MPMessage *)GetMsg(ReplyPort))
  297.         FreeVec(mpm);
  298.     }
  299.  
  300.     if(pubsig & signal)
  301.     {
  302.       SetSignal(0,pubsig);
  303.       ClosePubScreens();
  304.     }
  305.  
  306.     if(catchsig & signal)
  307.     {
  308.       while(catdnode=(struct DefaultNode *)GetMsg(CatchPort))
  309.       {
  310.         AddDNode(catdnode->Type,catdnode);
  311.       }
  312.       if(MPSem->CatchPopup)
  313.         ShowGUI();
  314.     }
  315.         
  316.     if(portsig & signal)
  317.     {
  318.       while(cxmsg=(CxMsg *)GetMsg(BrokerPort))
  319.       {
  320.         msgid=CxMsgID(cxmsg);
  321.         msgtype=CxMsgType(cxmsg);
  322.         ReplyMsg((struct Message *)cxmsg);     
  323.         switch(msgtype)
  324.         {
  325.           case CXM_IEVENT:
  326.             switch(msgid)
  327.             {
  328.               case EVT_HOTKEY:
  329.                 ShowGUI();
  330.                 break;
  331.               default:
  332.                 {
  333.                   struct OpenNode *on;
  334.                   
  335.                   ObtainSemaphoreShared(&MPSem->ListSem);
  336.                   on=(struct OpenNode *)MPSem->OpenList.lh_Head;
  337.                   while(on->on_Node.ln_Succ)
  338.                   {
  339.                     if(on=(struct OpenNode *)msgid)
  340.                     {
  341.                       ScreenToFront(on->Screen);
  342.                       DeleteCxObjAll(on->Cx);
  343.                       on->Cx=AddHotKey(Broker,BrokerPort,on->HotKey,(ULONG)on);
  344.                       break;
  345.                     }
  346.                     on=(struct OpenNode *)on->on_Node.ln_Succ;
  347.                   }
  348.                   ReleaseSemaphore(&MPSem->ListSem);
  349.                 }
  350.             }
  351.             break;
  352.           case CXM_COMMAND:
  353.             switch(msgid)
  354.             {
  355.               case CXCMD_DISABLE:
  356.                 MPSem->Enabled=FALSE;
  357.                 SendPrefsMsg(MP_REFRESH,0,0);
  358.                 break;
  359.               case CXCMD_ENABLE:
  360.                 MPSem->Enabled=TRUE;
  361.                 SendPrefsMsg(MP_REFRESH,0,0);
  362.                 break;
  363.               case CXCMD_KILL:
  364.                 CheckToQuit();
  365.                 break;
  366.               case CXCMD_UNIQUE:
  367.               case CXCMD_APPEAR:
  368.                 ShowGUI();
  369.                 break;
  370.               case CXCMD_DISAPPEAR:
  371.                 HideGUI();
  372.                 break;
  373.             }
  374.             break;        
  375.         }
  376.       }
  377.     }
  378.   }
  379.   HideGUI();
  380. }    
  381.  
  382. void ProcessMPMessages(void)
  383. {
  384.   struct MPMessage *mpm;
  385.  
  386.   while(mpm=(struct MPMessage *)GetMsg(MPSem->DaemonPort))
  387.   {
  388.     //printf("DaemonMsg Command %d  Code %d  Data %8x\n",mpm->Command,mpm->Code,mpm->Data);
  389.     
  390.     switch(mpm->Command)
  391.     {
  392.       case MP_QUIT:
  393.         CheckToQuit();
  394.         break;
  395.         /*
  396.       case MP_NEWNODE:
  397.         AddDNode(mpm->Code,mpm->Data);
  398.         break;
  399.       case MP_REMNODE:
  400.         RemDNode(mpm->Data);
  401.         break;
  402.         */      
  403.     }
  404.     ReplyMsg((struct Message *)mpm);
  405.   }
  406. }
  407.    
  408.  
  409. void ClosePubScreens(void)  
  410. {
  411.   struct OpenNode *on,*on2;
  412.   
  413.   ObtainSemaphore(&MPSem->OpenListSem);
  414.   
  415.   on=(struct OpenNode *)MPSem->OpenList.lh_Head;
  416.   
  417.   while(on->on_Node.ln_Succ)
  418.   {
  419.     if(on->Flags & ON_PUBLIC && !(on->Flags & ON_OPEN))
  420.     {
  421.       if(OldCloseScreen(on->Screen,IntuitionBase))
  422.       {
  423.         on2=(struct OpenNode *)on->on_Node.ln_Pred;
  424.         Remove((struct Node *)on);
  425.         FreeOpenNode(on);
  426.         on=on2;
  427.       }
  428.     }
  429.     on=(struct OpenNode *)on->on_Node.ln_Succ;
  430.   }
  431.  
  432.   ReleaseSemaphore(&MPSem->OpenListSem);
  433. }
  434.  
  435.  
  436. void CheckToQuit(void)
  437. {
  438.   APTR  CheckCloseScreen,
  439.         CheckOpenScreen,
  440.         CheckOpenScreenTagList,
  441.         CheckOpenWindow,
  442.         CheckOpenWindowTagList,
  443.         CheckCloseWorkBench,
  444.         CheckLayoutMenusA;
  445.   BOOL patchedover=FALSE,
  446.        screensopen=FALSE;
  447.   ULONG len;
  448.   UBYTE *warning;
  449.   UBYTE *warn;
  450.   
  451.   ObtainSemaphoreShared(&MPSem->ListSem);
  452.   ObtainSemaphore(&MPSem->OpenListSem);
  453.   Forbid();
  454.   
  455.   if(IsListEmpty(&MPSem->OpenList))
  456.   {
  457.     /**** check LVOs and make sure their mine ****/
  458.     /**** get LVOs,so put mine in thier, and get the result ****/ 
  459.     CheckCloseScreen        =SetFunction((struct Library *)IntuitionBase,-(0x42), (FUNCCAST)OldCloseScreen);
  460.     CheckOpenScreenTagList  =SetFunction((struct Library *)IntuitionBase,-(0x264),(FUNCCAST)OldOpenScreenTagList);
  461.     CheckOpenScreen         =SetFunction((struct Library *)IntuitionBase,-(0xc6), (FUNCCAST)OldOpenScreen);
  462.     CheckOpenWindowTagList  =SetFunction((struct Library *)IntuitionBase,-(0x25e),(FUNCCAST)OldOpenWindowTagList);
  463.     CheckOpenWindow         =SetFunction((struct Library *)IntuitionBase,-(0xcc), (FUNCCAST)OldOpenWindow);
  464.     CheckCloseWorkBench     =SetFunction((struct Library *)IntuitionBase,-(0x4e) ,(FUNCCAST)OldCloseWorkBench);
  465.     CheckLayoutMenusA       =SetFunction((struct Library *)GadToolsBase ,-(0x42), (FUNCCAST)OldLayoutMenusA);
  466.   
  467.     /**** now restore the LVOs to whatever they were ***/
  468.    
  469.     if((APTR)CheckCloseScreen       != (APTR)NewCloseScreen       ||
  470.        (APTR)CheckOpenScreen        != (APTR)NewOpenScreen        || 
  471.        (APTR)CheckOpenScreenTagList != (APTR)NewOpenScreenTagList ||
  472.        (APTR)CheckOpenWindow        != (APTR)NewOpenWindow        ||
  473.        (APTR)CheckOpenWindowTagList != (APTR)NewOpenWindowTagList ||
  474.        (APTR)CheckLayoutMenusA      != (APTR)NewLayoutMenusA)
  475.     {                 
  476.       SetFunction((struct Library *)IntuitionBase,-(0x42), (FUNCCAST)CheckCloseScreen);
  477.       SetFunction((struct Library *)IntuitionBase,-(0x264),(FUNCCAST)CheckOpenScreenTagList);
  478.       SetFunction((struct Library *)IntuitionBase,-(0xc6) ,(FUNCCAST)CheckOpenScreen);
  479.       SetFunction((struct Library *)IntuitionBase,-(0x25e),(FUNCCAST)CheckOpenWindowTagList);
  480.       SetFunction((struct Library *)IntuitionBase,-(0xcc) ,(FUNCCAST)CheckOpenWindow);
  481.       SetFunction((struct Library *)IntuitionBase,-(0x4e) ,(FUNCCAST)CheckCloseWorkBench);  
  482.       SetFunction((struct Library *)GadToolsBase ,-(0x42) ,(FUNCCAST)CheckLayoutMenusA);
  483.       
  484.       patchedover=TRUE;
  485.     }
  486.     else
  487.     {
  488.       Deinstall=TRUE;
  489.       SendPrefsMsg(MP_QUIT,0,0);
  490.     }
  491.   }
  492.   else
  493.   {
  494.     struct OpenNode *on;
  495.   
  496.     warn=GetString(MSG_REQ_CANT_QUIT_SCREENS);
  497.     
  498.     len=strlen(warn);
  499.     
  500.     on=(struct OpenNode *)MPSem->OpenList.lh_Head;
  501.     while(on->on_Node.ln_Succ)
  502.     {
  503.       len+=strlen(on->Screen->Title)+4;
  504.       on=(struct OpenNode *)on->on_Node.ln_Succ;
  505.     }
  506.     if(warning=AllocVec(len,MEMF_PUBLIC|MEMF_CLEAR|MEMF_NO_EXPUNGE))    
  507.     {
  508.       strcpy(warning,warn);
  509.       on=(struct OpenNode *)MPSem->OpenList.lh_Head;
  510.       while(on->on_Node.ln_Succ)
  511.       {
  512.         strcat(warning,"\n  ");
  513.         strcat(warning,on->Screen->Title);
  514.         on=(struct OpenNode *)on->on_Node.ln_Succ;
  515.       }
  516.       screensopen=TRUE;
  517.     }
  518.   }
  519.   ReleaseSemaphore(&MPSem->OpenListSem);
  520.   ReleaseSemaphore(&MPSem->ListSem);
  521.   
  522.   Permit();
  523.   
  524.   if(patchedover)
  525.     EZReq(0,0,ModePro,GetString(MSG_REQ_CANT_QUIT_PATCHED_OVER),GetString(MSG_REQ_OK),0);   
  526.   
  527.   if(screensopen && warning)
  528.   {
  529.     EZReq(0,0,ModePro,warning,GetString(MSG_REQ_OK),0);
  530.     FreeVec(warning);
  531.   }
  532. }
  533.  
  534. LONG SendPrefsMsg(ULONG Command, ULONG Code, APTR Data)
  535. {
  536.   LONG rv=-1;
  537.   struct MPMessage *mpm;
  538.  
  539.   if(AttemptSemaphore(&MPSem->PortSem))
  540.   {
  541.     if(MPSem->PrefsPort)
  542.     {
  543.       rv=0;
  544.       if(mpm=AllocVec(sizeof(struct MPMessage),MEMF_PUBLIC|MEMF_CLEAR))
  545.       {
  546.         mpm->Command=Command;
  547.         mpm->Code=Code;
  548.         mpm->Data=Data;
  549.         PutMsg(MPSem->PrefsPort,(struct Message *)mpm);
  550.         rv=1;
  551.       }
  552.     }
  553.     ReleaseSemaphore(&MPSem->PortSem);
  554.   }
  555.   return(rv);
  556. }
  557.  
  558. BOOL DaemonLoadFile(STRPTR FileName)
  559. {
  560.   LONG error;
  561.   BOOL rv;
  562.  
  563.   // no need for protection, the lists are not public
  564.   // when this function is called.  
  565.   rv=LoadFile(MPSem->PromotionList,FileName);
  566.   if(error=IoErr())
  567.   {
  568.     UBYTE errstr[91];
  569.  
  570.     Fault(error,GetString(MSG_REQ_LOAD_ERROR),errstr,90);
  571.     EZReq(0,0,ModePro,errstr,GetString(MSG_REQ_OK),0);
  572.   }
  573.   return(rv);
  574. }    
  575.